package cn.isaac.firstkotlin.practice

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * Created by RaoWei on 2017/5/23 10:39.
 */

object DelegatesExt {
    fun <T> notNullSingleValue() = NotNullStringValueVar<T>()
}

class NotNullStringValueVar<T>() : ReadWriteProperty<Any?, T> {
    private var value: T? = null
    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value ?: throw IllegalStateException("${property.name} not initialized")
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        this.value = if (this.value == null) value else throw IllegalStateException("${property.name} already initialized")
    }
}